package com.dhecp.core.utils;

import com.dhecp.common.core.page.PageDomain;
import com.dhecp.common.core.page.TableSupport;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.common.utils.sql.SqlUtil;
import com.dhecp.framework.util.ShiroUtils;
import com.github.pagehelper.PageHelper;

/**
 * 分页工具类
 *
 * @author alula
 */

public class PageUtils {
	/**
	 * 常规单字段排序，针对前端传递的排序字段和规则
	 */
    public static void startPage(){
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)){
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    /**
     * 按传入字段和规则排序，如：传入orderBy = "menuGuid desc, moduleOrder asc" (参考功能字典的使用)
     * @param orderBy
     * @author DH-Xxj
     */
    public static void startPageOrder(String orderBy){
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)){
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
}
